package com.wjw.redis.stream.config;

import com.wjw.redis.stream.constant.CacheConstant;
import com.wjw.redis.stream.model.UserInfo;
import com.wjw.redis.stream.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserInfoStreamMessageListener implements StreamListener<String, ObjectRecord<String, UserInfo>> {

    private final RedisUtil redisUtil;

    public UserInfoStreamMessageListener(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public void onMessage(ObjectRecord<String, UserInfo> message) {
        try {
            String stream = message.getStream();
            RecordId id = message.getId();
            UserInfo messageValue = message.getValue();
            log.info("UserInfoStreamMessageListener group:[group-c] 接收到一个消息 stream:[{}],id:[{}],value:[{}]", stream, id, messageValue);
            long ackResult = redisUtil.ack(CacheConstant.REDIS_STEAM_02, "group-c", id.getValue()); //手动ack
            log.info("UserInfoStreamMessageListener 确认结果：{}", ackResult);
//        long result = redisUtil.del(stream, id.getValue());
//        log.info("UserInfoStreamMessageListener 删除结果：{}", result);
        } catch (Exception e) {
            log.error("处理消息时发生异常：", e);
        }


    }
}